package com.tc.controller;

import java.io.UnsupportedEncodingException;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;

import cn.apiclub.captcha.Captcha;
import com.tc.base.shiro.UserContext;
import com.tc.service.SysUserService;
import com.tc.util.GlobalKeys;
import org.apache.shiro.SecurityUtils;
import org.apache.shiro.authc.UsernamePasswordToken;
import org.apache.shiro.subject.Subject;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.servlet.ModelAndView;


/**
 * Created by Administrator on 2016/1/15 0015.
 */
@Controller
@RequestMapping("/user")
public class SysUserController {

	@Autowired
	SysUserService sysUserService;


	@RequestMapping(value = "/login")
	public String login(){
		return "login";
	}

	@RequestMapping(value = "/main")
	public ModelAndView main(HttpSession session, HttpServletRequest req) {
		ModelAndView modelView = new ModelAndView();
		Subject currentUser = SecurityUtils.getSubject();
		modelView.addObject("loginError", true);
		modelView.addObject("errorMsg", "验证码错误");
		modelView.setViewName("/login");
		try{
			if(!currentUser.isAuthenticated()){
				String username = req.getParameter("username");
				String password = req.getParameter("password");

				String captcha = req.getParameter(GlobalKeys.VALIDATE_CODE_NAME);
				Captcha sessionCaptcha = (Captcha) req.getSession().getAttribute(GlobalKeys.SESSION_CAPTCHA);
				if(!sessionCaptcha.isCorrect(captcha)){
					modelView.addObject("loginError", true);
					modelView.addObject("errorMsg", "验证码错误");
							modelView.setViewName("/login");
					return modelView;
				}

				UsernamePasswordToken token = new UsernamePasswordToken(
						username, password);
				//token.setRememberMe(true);
				currentUser.login(token);

				if(currentUser.isAuthenticated()){
					Map<String,Object> user = sysUserService.getUserByUserId(username);
					session.setAttribute("userinfo", user);
					return new ModelAndView("redirect:/user/home.do");
				}
			}
		}catch(Exception e){
			currentUser.logout();
			modelView.addObject("loginError", true);
			modelView.addObject("errorMsg", e.getMessage());

			return modelView;
		}
		currentUser.logout();
		return modelView;
	}

	@RequestMapping("/home")
	public String home(){
		return "main";
	}




	@RequestMapping("/login2")
	public String login2(HttpServletRequest req, HttpServletResponse resp){

		String username = req.getParameter("username");
		String password = req.getParameter("password");
		String captcha = req.getParameter(GlobalKeys.VALIDATE_CODE_NAME);

		Captcha sessionCaptcha = (Captcha) req.getSession().getAttribute(GlobalKeys.SESSION_CAPTCHA);
		if(!sessionCaptcha.isCorrect(captcha)){
			req.setAttribute("captchaError",1);
			return "login";
		}

		Map<String,Object> user = sysUserService.getUserByUserIdPassword(username,password);
		if (user == null){
			req.setAttribute("userError",1);
			return "login";
		}

		System.out.println("login success");
		return "redirect:/main/index";

	}

	@RequestMapping(params = "/logout")
	public ModelAndView logout(HttpSession session, HttpServletRequest req) {
		ModelAndView modelView = new ModelAndView();
		modelView.setViewName("/login");
		Subject currentUser = SecurityUtils.getSubject();
		currentUser.logout();
		return modelView;

	}
}
